package com.cxs.chain.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author: cxs
 */

@Service
public class ChainSpringTest implements ApplicationContextAware {

    private AbstractSpringHandler entryHandler;


    public Object doSth() {
        System.out.println("执行业务开始");
        return entryHandler.doHandler();
    }

    /**
     * 这里给整合
     *
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, AbstractSpringHandler> abstractSpringHandlerMap = applicationContext.getBeansOfType(AbstractSpringHandler.class);
        // 这里拿到所有的过滤器，组合顺序 可以按照业务顺序组合
        AbstractSpringHandler aSpringHandler = abstractSpringHandlerMap.get("ASpringHandler");
        AbstractSpringHandler bSpringHandler = abstractSpringHandlerMap.get("BSpringHandler");

        aSpringHandler.setNextHandler(bSpringHandler);

        this.entryHandler = aSpringHandler;
    }
}
